package com.ruoyi.project.app.aspect.handler;

import com.ruoyi.project.app.anno.CheckEmail;
import com.ruoyi.project.app.utils.RegexValidate;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

public class CheckEmailHandler implements Handler{

    @Override
    public boolean handler(AnnotatedElement element, Object obj) {
        Class<?> clazzType = null;
        CheckEmail checkEmail = null;
        if(element instanceof Parameter){
            Parameter  parameter = (Parameter) element;
            clazzType = parameter.getType();
            checkEmail = parameter.getAnnotation(CheckEmail.class);
        }else if(element instanceof Field){
            Field field = (Field) element;
            clazzType = field.getType();
            checkEmail = field.getAnnotation(CheckEmail.class);
        }
        if(clazzType==String.class&&checkEmail != null&&!RegexValidate.validationEmail((String)obj)){
            throw new RuntimeException(checkEmail.msg());
        }
        return false;
    }
}
